import numpy as np
import zt.ML.utils as mltls
from sklearn import svm


def load(path):
    D = mltls.MLData()
    _X = []; _Y = []
    with open(path, 'r') as f:
        lines = f.readlines()
    for line in lines:
        l = line.split()
        # x, y = [1, ] + list(map(float, l[:-1])), int(l[-1])
        x = list(map(float, l[1:]))
        y = 1 if float(l[0]) == 8 else -1
        _X.append(x)
        _Y.append(y)
    mltls.MLData.initialize(D, _X, _Y)
    return D

def error(A, T):
    err = 0
    for i in range(T.size):
        x, y = T.choice(i)
        if sign(A.predict(x)) != y:
            err += 1
    return err / T.size

D = load("./data/features.train.txt")
T = load("./data/features.test.txt")

def sign(x):
    return int(x / abs(x))

def kernel(x1, x2):
    return (1 - x1.dot(x2.T)) ** 2

model = svm.SVC(gamma=10000, C=0.1, verbose=True)
model.fit(D.X, D.Y)
# ein = error(model, D)
eout = error(model, T)
print(eout)